﻿using BCCommon;
using BCCommon.Enums;
using BCCommon.Extensions;
using BCData.Common.PushMessageConfig;
using BCDto.Common.PushMessageConfig;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.PushMessageConfig;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BCService.Common.PushMessage.PushMessageConfig
{
    public class PushMessageConfigService : IPushMessageConfigService
    {
        private readonly IPushMessageConfigData pushMessageConfigData;

        public PushMessageConfigService(IPushMessageConfigData pushMessageConfigData)
        {
            this.pushMessageConfigData = pushMessageConfigData;
        }

        public PushMessageConfigDto Add(AdminDto adminDto, PushMessageConfigRequestDto pushMessageConfigRequestDto)
        {
            var existed = this.pushMessageConfigData.CheckPushMessageConfigExists(pushMessageConfigRequestDto.PushMessageType).GetAwaiter().GetResult();
            if (existed != null)
            {
                throw new ArgumentException("推送消息类型已存在");
            }
            //TODO 接入字典
            if (pushMessageConfigRequestDto.Ranges > 50)
            {
                throw new ArgumentException("推送消息范围不能超过50");
            }
            PushMessageConfigEntity pushMessageConfigEntity = pushMessageConfigRequestDto.As<PushMessageConfigEntity>();
            pushMessageConfigEntity.Editer = adminDto.FullName;
            pushMessageConfigEntity.PushMessageName = pushMessageConfigRequestDto.PushMessageType.GetDisplayString();
            var entity = this.pushMessageConfigData.Add(pushMessageConfigEntity).GetAwaiter().GetResult();
            if (entity != null)
            {
                return entity.As<PushMessageConfigDto>();
            }
            return null;
        }

        public PushMessageConfigDto GetEntity(PushMessageType pushMessageType)
        {
            return this.pushMessageConfigData.GetEntity(pushMessageType).GetAwaiter().GetResult().As<PushMessageConfigDto>();
        }

        public async Task<PushMessageConfigDto> GetById(int pushMessageConfigId)
        {
            var data = await pushMessageConfigData.GetById(pushMessageConfigId);
            return data.As<PushMessageConfigDto>();
        }

        public PageableList<PushMessageConfigDto> Query(DateTime? start, DateTime? end, bool? ascending, int pageIndex, int pageSize)
        {
            var (list, count) = this.pushMessageConfigData.Query(start, end, ascending, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<PushMessageConfigDto>()
            {
                Items = list.As<IEnumerable<PushMessageConfigDto>>(),
                Count = count
            };
        }

        public PushMessageConfigDto Update(AdminDto adminDto, int pushMessageConfigId, PushMessageConfigRequestDto pushMessageConfigRequestDto)
        {
            var entity = this.pushMessageConfigData.GetEntity(pushMessageConfigRequestDto.PushMessageType).GetAwaiter().GetResult();
            if (entity != null)
            {
                if (entity.PushMessageConfigId != pushMessageConfigId)
                {
                    throw new ArgumentException(string.Format("推送消息类型 {0} 已存在", pushMessageConfigRequestDto.PushMessageType.GetDisplayString()));
                }
            }
            var exists = this.pushMessageConfigData.CheckPushMessageConfigExists(pushMessageConfigId).GetAwaiter().GetResult();
            if (exists == null)
            {
                throw new ArgumentException("推送消息配置信息不存在");
            }
            if (pushMessageConfigRequestDto.Ranges > 50)
            {
                throw new ArgumentException("推送消息范围不能超过50");
            }
            PushMessageConfigEntity pushMessageConfigEntity = pushMessageConfigRequestDto.As<PushMessageConfigEntity>();
            pushMessageConfigEntity.PushMessageConfigId = pushMessageConfigId;
            pushMessageConfigEntity.Editer = adminDto.FullName;
            pushMessageConfigEntity.PushMessageName = pushMessageConfigRequestDto.PushMessageType.GetDisplayString();
            var entityResult = this.pushMessageConfigData.Update(pushMessageConfigEntity).GetAwaiter().GetResult();
            return entityResult?.As<PushMessageConfigDto>();
        }
    }
}